﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;

namespace Telecom.Service
{
    public class ODFService
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();

        public ODFService()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public decimal addorUpdate(ODF entity)
        {
            if (entity.ID == 0) dbContext.ODF.Add(entity);
            else dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            dbContext.SaveChanges();
            return entity.ID;
        }

        public List<ODF> getList(int siteId)
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            try
            {
                return dbContext.ODF.Where(p => p.SITEID == siteId).ToList();
            }
            catch (Exception e)
            {
                List<ODF> _listRe = new List<ODF>();
                return _listRe;
            }
        }

        


        public ODF GetbyId(int ma)
        {
            try
            {
                return dbContext.ODF.Where(p => p.ID == ma).First();
            }
            catch (Exception e)
            {
                ODF _listRe = new ODF();
                return _listRe;
            }
        }

        public ODF GetbyStringMa(string ma)
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            try
            {
                return dbContext.ODF.Where(p => p.MA == ma).First();
            }
            catch (Exception e)
            {
                ODF _listRe = new ODF();
                return _listRe;
            }
        }

        public List<ODF_SOIQUANG> getListODFSoiQuang(int OdfId)
        {
            try
            {
                return dbContext.ODF_SOIQUANG.Where(p => p.ODFID == OdfId).OrderBy(p => p.PORT).ToList();
            }
            catch (Exception e)
            {
                List<ODF_SOIQUANG> _listRe = new List<ODF_SOIQUANG>();
                return _listRe;
            }
        }

        public SOIQUANG GetSoiQuangById(int ma)
        {
            try
            {
                return dbContext.SOIQUANG.Where(p => p.ID == ma).First();
            }
            catch (Exception e)
            {
                SOIQUANG _listRe = new SOIQUANG();
                return _listRe;
            }
        }

 
    }
}
